######################### Load Libraries #######################################

library(foreign)

library(grDevices)

library(ggplot2)

library(lattice)

library(plyr)

library(gridExtra)

library(gdata)

############################ Load Data #########################################

# set working directory
setwd("~/_data/")

# load LPR data set that includes variables from other data sets
load("lprdata_plus.RData")

# remove unnecessary objects from the R image
keep(all, sure=TRUE)

# remove Central and Eastern European countries for which we don't have LPR measure yet
all <- all[-which(all$isocode %in% c("ALB","ARM","BGR","CHE","CZE","EST","GEO","HRV","HUN","LTU","LVA","MDA","POL","ROU","RUS","SVN","UKR")),]


############################## Graphs/Tables ###################################
############################### Manuscript #####################################

################################### MS Table 2 #################################

# separate file; use "prices_analysis.R"

################################################################################

################################## MS Figure 1 #################################

# set working directory for MS graphs
setwd("~/_manuscript_tabfig/")

# remove existing variable "orderlpr"
all <- all[,-57]

# generate new data that ranks countries by median LPR
lpr_sum <- ddply(all,~Country,summarize,median=median(lossprob_statictau,na.rm=T))
lpr_sum <- lpr_sum[order(lpr_sum[,2]),]
lpr_sum$orderlpr <- seq(1,nrow(lpr_sum))

# merge ranked median LPR data with main data set
all <- merge(all,lpr_sum,by="Country")

# generate graph
p <- ggplot(data = all, aes(y = lossprob_statictau, x = as.factor(orderlpr))) + geom_boxplot(aes(fill=smdF)) + ylab("Loss Probability") + xlab("Country") + theme(legend.position="none") + theme_bw() + theme(legend.position="none") + scale_x_discrete(breaks=seq(1,23), labels=unique(all[order(all$orderlpr),c("isocode","orderlpr")])$isocode) + theme(axis.text.x=element_text(size=7)) + scale_fill_manual(values = c("red3", "lightblue"))

# save graph to eps file
ggsave(file="lpr_box_country_smd.eps")

# save graph to pdf file
pdf("lpr_box_country_smd.pdf")
p
dev.off()

################################################################################

################################## MS Figure 2 #################################

# separate file; use "prices_analysis.R"

################################################################################

################################## MS Figure 3 #################################

# set working directory for MS graphs
setwd("~/_manuscript_tabfig/")

# generate plurality party change probability variable
all$chprob2 <- rep(NA,nrow(all))

for (j in sort(unique(all$Country))) {

		for (i in 2:length(all[all$Country==j,"chprob2"])) {

	all[all$Country==j,"chprob2"][i] <- mean(all[all$Country==j,"change"][1:i],na.rm=T)
		}

}

# generate graph
p <- ggplot(data = all, aes(y = lossprob_statictau, x = chprob2, colour=country)) + geom_point()

# generate final graph
p +  stat_smooth(aes(group = 1), , colour="grey30", method = "lm", formula = y ~ x, se = FALSE) + stat_smooth(aes(group = 1), , colour="grey60", method = "loess", formula = y ~ x, se = FALSE) + xlab("Plurality Party Change Probability") + ylab("Loss Probability") + theme_bw() + theme(legend.position="none")

# save graph to eps file
ggsave(file="lpr_v_change.eps")

# save graph to pdf file
pdf("lpr_v_change.pdf")
p +  stat_smooth(aes(group = 1), , colour="grey30", method = "lm", formula = y ~ x, se = FALSE) + stat_smooth(aes(group = 1), , colour="grey60", method = "loess", formula = y ~ x, se = FALSE) + xlab("Plurality Party Change Probability") + ylab("Loss Probability") + theme_bw() + theme(legend.position="none")
dev.off()

################################################################################
################################################################################


############################## Graphs/Tables ###################################
########################## Supporting Materials ################################

################################## SM Figure 1 #################################

# separate file; use "can_swing_time.R"

################################################################################

################################## SM Figure 2 #################################

# separate file; use "pr_code_limitcase_aus06.R"

################################################################################

################################## SM Figure 3 #################################

# separate file; use "pr_code_limitcase_esp96.R"

################################################################################

################################## SM Figure 4 #################################

# separate file; use "lpr_density_variation.R"

################################################################################

################################## SM Figure 5 #################################

# set working directory for SM graphs
setwd("~/_supportingmaterials_tabfig/")

# subset data to include only observations with LPR values, relevant variables and to exclude Switzerland
lpr_change <- all[is.na(all$lossprob_statictau)==F,]
lpr_change <- lpr_change[,c("country","year","change","chprob","lossprob_statictau")]
lpr_change <- lpr_change[lpr_change$country!="Switzerland",]

# generate pdf graph (color)
pdf("lprchange.pdf")
xyplot(as.factor(change) ~ lossprob_statictau | country, lpr_change, panel = panel.bwplot, xlab = "LPR", ylab = "Government Change Indicator")
dev.off()

# generate pdf graph (black and white)
pdf("lprchange_bw.pdf")
xyplot(as.factor(change) ~ lossprob_statictau | country, lpr_change, panel = panel.bwplot, par.settings=list(strip.background=list(col=c(0)),box.rectangle=list(col="black"),box.umbrella = list(col = "black"),box.dot=list(col="black"),plot.symbol=list(col="black")), xlab = "LPR", ylab = "Government Change Indicator")
dev.off()

################################################################################

################################## SM Figure 6 #################################

# separate file; use "tau_bootstrap.R"

################################################################################

################################## SM Figure 7 #################################

# set working directory for SM graphs
setwd("~/_supportingmaterials_tabfig/")

# generate and save main graph
p <- ggplot(data = all, aes(y = abs(vmarginall), x = enep1, colour=country)) + geom_point()

# save graph to eps file
ggsave(file="vmargin_enep1.eps")

# generate final graph
pdf("vmargin_enep1.pdf")
p + stat_smooth(aes(group = 1), colour="grey30", method = "lm", formula = y ~ x, se = FALSE) + stat_smooth(aes(group = 1), colour="grey60", method = "loess", formula = y ~ x, se = FALSE) + xlab("Effective Number of Electoral Parties (ENEP1)") + ylab("Vote Margin") + theme_bw() + theme(legend.position="none")
dev.off()

# generate and save main graph
p <- ggplot(data = all, aes(y = van_comp, x = enep1, colour=country)) + geom_point()

# generate final graph
p +  stat_smooth(aes(group = 1), colour="grey30", method = "lm", formula = y ~ x, se = FALSE) + stat_smooth(aes(group = 1), colour="grey60", method = "loess", formula = y ~ x, se = FALSE) + xlab("Effective Number of Electoral Parties (ENEP1)") + ylab("Vanhanen Competitiveness") + theme_bw() + theme(legend.position="none")

# save graph to eps file
ggsave(file="vanhannen_v_ENEP1.eps")

# save graph to pdf file
pdf("vanhannen_v_ENEP1.pdf")
p +  stat_smooth(aes(group = 1), colour="grey30", method = "lm", formula = y ~ x, se = FALSE) + stat_smooth(aes(group = 1), colour="grey60", method = "loess", formula = y ~ x, se = FALSE) + xlab("Effective Number of Electoral Parties (ENEP1)") + ylab("Vanhanen Competitiveness") + theme_bw() + theme(legend.position="none")
dev.off()

################################################################################

################################## SM Figure 8 #################################

# set working directory for SM graphs
setwd("~/_supportingmaterials_tabfig/")

# generate and save main graph
p <- ggplot(data = all, aes(y = lossprob_statictau, x = enep1, colour=country)) + geom_point()

# generate final graph
p +  stat_smooth(aes(group = 1), colour="grey30", method = "lm", formula = y ~ x, se = FALSE) + stat_smooth(aes(group = 1), colour="grey60", method = "loess", formula = y ~ x, se = FALSE) + xlab("Effective Number of Electoral Parties (ENEP1)") + ylab("Loss Probability") + theme_bw() + theme(legend.position="none")

# save graph to eps file
ggsave(file="lpr_v_ENEP1.eps")

# save graph to pdf file
pdf("lpr_v_ENEP1.pdf")
p +  stat_smooth(aes(group = 1), colour="grey30", method = "lm", formula = y ~ x, se = FALSE) + stat_smooth(aes(group = 1), colour="grey60", method = "loess", formula = y ~ x, se = FALSE) + xlab("Effective Number of Electoral Parties (ENEP1)") + ylab("Loss Probability") + theme_bw() + theme(legend.position="none")
dev.off()

################################################################################
